package com.ruoyi.project.user.host.service.impl;

import com.ruoyi.project.system.user.domain.User;
import com.ruoyi.project.system.user.service.IUserService;
import com.ruoyi.project.user.host.domain.Host;
import com.ruoyi.project.user.host.mapper.HostMapper;
import com.ruoyi.project.user.host.service.HostService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * 主持人  业务层逻辑
 */
@Service
public class HostServiceImpl implements HostService {

    @Autowired
    private HostMapper hostMapper;

    /**
     * 主持人崗位在數據庫中的主鍵值
     */
    @Value("${my.mysql.post.hostId}")
    private Long hostId;


    /**
     * 在开发过程中不要随意调用别人写好的mapper层
     * 如果有需要 要提前跟人家说好  调用service层
     * 标准项目开发
     */

    @Autowired
    private IUserService iUserService;

    /**
     * 查询全部
     *
     * @return
     */
    @Override
    public List<Host> selectHost(Host host) {
        return hostMapper.selectHost(host);
    }

    /**
     * 查询手机是否存在
     *
     * @param hostPhone
     * @return
     */
    @Override
    public Long selectByPhone(String hostPhone) {

        return hostMapper.selectByPhone(hostPhone);

    }

    /**
     * 查询邮箱是否存在
     *
     * @param hostEmail
     * @return
     */
    @Override
    public Long selectByEmail(String hostEmail) {

        return hostMapper.selectByEmail(hostEmail);
    }

    /**
     * 新增主持人
     *
     * @param host
     * @param password
     * @return
     */

    @Transactional
    @Override
    public int insertHost(Host host, String password) {
        User user = new User();
        Date date = new Date();

        user.setPassword(password);
        user.setEmail(host.getHostEmail());
        user.setPhonenumber(host.getHostPhone());
        user.setLoginName(host.getHostPhone());
        user.setUserName(host.getHostName());
        user.setCreateTime(date);

        Long[] hostids = {hostId};

        user.setPostIds(hostids);

        int index = iUserService.insertUser(user);

        if (index > 0) {

            host.setHostUserId(user.getUserId());
            host.setHostRegtime(date);
            int i = hostMapper.insertHost(host);
            if (i > 0) {
                return 1;
            }
        }
        return 0;
    }

    /**
     * 根据主键修改权重
     *
     * @param host
     * @return
     */
    @Override
    public int updateHostSortWeight(Host host) {
        return hostMapper.updateHost(host);
    }

    /**
     * 根据排序导出表
     *
     * @param host
     * @param sortTable
     * @return
     */
    @Override
    public List<Host> exportSort(Host host, int sortTable) {

        return hostMapper.selectHostOrderWeight(host, sortTable);

    }

    /**
     * 批量删除指定主持人
     * @param ids
     * @return
     */
    @Transactional
    @Override
    public int deleteHostByIds(Long[] ids) {
        int index2 = 0;
        for (Long id : ids) {
            Host host = hostMapper.selectByHostUserId(id);
            index2 += iUserService.deleteUserById(host.getHostUserId());

        }

        if (ids.length == index2) {
            int index = hostMapper.deleteHostByIds(ids);
            return index;
        }

        return 0;
    }

    /**
     * 根据id查询用户
     * @param hostId
     * @return
     */
    @Override
    public Host selectHostById(Long hostId) {
        return hostMapper.selectByHostUserId(hostId);
    }

    /**
     * 修改主持人
     * @param host
     * @return
     */
    @Transactional
    @Override
    public int updateHostCheck(Host host) {

        int index = hostMapper.updateHost(host);
        if(index == 1){
            Host host1 = hostMapper.selectByHostUserId(host.getHostId());
            User user = new User();
            user.setUserId(host1.getHostUserId());
            user.setPhonenumber(host1.getHostPhone());
            user.setUserName(host1.getHostName());
            user.setLoginName(host1.getHostPhone());
            user.setEmail(host1.getHostEmail());
            int index2 = iUserService.updateUser(user);
            if(index2 == 1){
                return index2;
            }
        }
        return 0;
    }

    /**
     * 修改状态
     * @param host
     * @return
     */
    @Override
    public int updateState(Host host) {
        System.out.println("=======Service=======before"+host);
        System.out.println("=======Service=======after"+hostMapper.updateHost(host));
        return hostMapper.updateHost(host);
    }


}
